package com.ssy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssy.common.CodeMsg;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import com.ssy.system.mapper.NoticeMapper;
import com.ssy.system.pojo.Notice;
import com.ssy.system.service.INoticeService;
import com.ssy.system.vo.NoticeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * @author ssy
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public PageResult loadAllNoticePage(NoticeVo noticeVo) {
        Page<Notice> page = new Page<>(noticeVo.getPage(), noticeVo.getLimit());
        IPage<Notice> noticeIPage = this.noticeMapper.selectPage(page, new LambdaQueryWrapper<Notice>()
                .like(StringUtils.hasText(noticeVo.getTitle()), Notice::getTitle, noticeVo.getTitle())
                .like(StringUtils.hasText(noticeVo.getCreateName()), Notice::getCreateName, noticeVo.getCreateName())
                .eq(noticeVo.getType() != null, Notice::getType, noticeVo.getType())
                .orderByDesc(Notice::getCreateTime));
        return new PageResult(noticeIPage.getTotal(), noticeIPage.getRecords());
    }

    @Override
    public Result insertNotice(Notice notice) {
        if (notice == null) {
            return new Result(CodeMsg.ADD_ERROR);
        }
        int i = this.noticeMapper.insert(notice);
        return i > 0 ? new Result(CodeMsg.ADD_SUCCESS) : new Result(CodeMsg.ADD_ERROR);
    }

    @Override
    public Result updateNotice(Notice notice) {
        if (notice == null) {
            return new Result(CodeMsg.UPDATE_ERROR);
        }
        int i = this.noticeMapper.updateById(notice);
        return i > 0 ? new Result(CodeMsg.UPDATE_SUCCESS) : new Result(CodeMsg.UPDATE_ERROR);
    }

    @Override
    public Result deleteNoticeById(Integer id) {
        if (id == null) {
            return new Result(CodeMsg.DELETE_ERROR);
        }
        int i = this.noticeMapper.deleteById(id);
        return i > 0 ? new Result(CodeMsg.DELETE_SUCCESS) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result bathDeleteNotice(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return new Result(CodeMsg.DELETE_ERROR);
        }
        ArrayList<Serializable> idList = new ArrayList<>();
        for (Integer id : ids) {
            idList.add(id);
        }
        this.noticeMapper.deleteBatchIds(idList);
        return new Result(CodeMsg.DELETE_SUCCESS);
    }
}
